home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5932 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  84 lines

  1. Path: newshost.cyberramp.net!news
  2. From: sinan@cyberramp.net (John Noland)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Packing and Unpacking Numeric Data
  5. Date: 22 Feb 1996 01:17:32 GMT
  6. Organization: Prose Software
  7. Message-ID: <4ggg7c$kpg@newshost.cyberramp.net>
  8. References: <31250EE4.3642@cybercomm.net>
  9. NNTP-Posting-Host: ramp1-23.cyberramp.net
  10. X-Newsreader: WinVN 0.99.5
  11.  
  12. In article <31250EE4.3642@cybercomm.net>, fschitto@cybercomm.net says...
  13. >
  14. >This is our situation.
  15. >
  16. >We need to transfer data from Unix/Oracle/C to MVS cobol and from MVS/Cobol 
  17. >to Unix/Oracle/C.  We are being asked to transfer the data to MVS from UNIX 
  18. >in a format that MVS/COBOL can understand.  The problem is with MVS Cobol 
  19. >COMP-3 fields and Zoned decimal.  My question is, Is their a utility in C, on 
  20. >the UNIX side, which will convert numeric data to Packed Decimal or Zoned 
  21. >Decimal and also convert back from Packed Decimal or Zoned Decimal to Numeric 
  22. >data.
  23. >
  24. >Please note that may programming skills with C are not that great. 
  25. >
  26. >Any Help in this matter would be greatly appreciated.
  27.  
  28. This probably isn't the right place for you to post this. But, too 
  29. late now.
  30.  
  31. Here are a couple of routines that I did for a COBOL to C conversion.
  32. Note that these routines are for DOS/Intel. Your int's will be 32 bit+
  33. instead of 16 bit, and the byte storage on your computer may differ.
  34. The principal should be the same though. I included the comp-5 routine
  35. just to illustrate how to handle odd byte sizes. The comp5_long() 
  36. routine takes a pointer to 3-byte character array.
  37. In my opinion, the conversion is easier from the COBOL side. I would
  38. suggest that you determine the COBOL data types that correspond to
  39. the standard C types. For example, PIC S9(9) comp-5 would correspond 
  40. to a 32 bit integer (long for me, probably int for you). Once you've 
  41. figured out the corresponding data types, a simple move from a comp-3 
  42. field to a comp-5 will convert it and vice-versa.
  43.  
  44.  
  45. /*********************** comp3_int **************************************
  46. Converts a COBOL 2 byte comp-3 (3 digit packed decimal) to a 2 byte integer.
  47. ************************************************************************/
  48.  
  49. int comp3_int(int convertee){
  50.  
  51. int packdec1, packdec2, packdec3;
  52.  
  53.    packdec1 = convertee; /* Decimal 100's place */
  54.    packdec2 = convertee; /* Decimal 10's place */
  55.    packdec3 = convertee; /* Decimal 1's place */
  56.    packdec1 &= 0xF0;     
  57.    packdec1 >>= 4;       
  58.    packdec1 *= 100;      
  59.    packdec2 &= 0xf;
  60.    packdec2 *= 10;
  61.    packdec3 >>= 12;
  62.    return(packdec1 + packdec2 + packdec3);
  63. }
  64.  
  65.  
  66. /************************ comp5_long ***********************************
  67. Converts a COBOL 3 byte comp-5 to a 4 byte long integer.
  68. ***********************************************************************/
  69.  
  70. long comp5_long(char *s){
  71.  
  72. union {
  73.    char c_num[4];
  74.    long l_num;
  75. } r_num_cnv;
  76.  
  77.    r_num_cnv.c_num[0] = s[0];
  78.    r_num_cnv.c_num[1] = s[1];
  79.    r_num_cnv.c_num[2] = s[2];
  80.    r_num_cnv.c_num[3] = 0;
  81.    return(r_num_cnv.l_num);
  82. }
  83.  
  84.